package com.zyy.ui.validations;

import com.zyy.ui.annotations.Region;
import com.zyy.ui.utils.StringUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: RegionValidator
 * @Description: 范围校验
 * @Author: zyy
 * @Date: 2020/05/03 16:01
 * @Version: 1.0
 */
public class RegionValidator implements ConstraintValidator<Region, String> {

    private List<String> valueList;

    @Override
    public void initialize(Region constraintAnnotation) {
        valueList = Arrays.asList(constraintAnnotation.value());
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtil.isBlank(value)) {
            return true;
        }

        if (valueList.contains(value)) {
            return true;
        }
        return false;
    }
}
